package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.AuthDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.LoginDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserService;
import com.heima.utils.common.AppJwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
@Slf4j
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {


    @Override
    public ResponseResult login(LoginDto loginDto) {
        //1.存在账号和密码，正常登录
        if (StringUtils.isNoneBlank(loginDto.getPhone()) && StringUtils.isNoneBlank(loginDto.getPassword())) {
            LambdaQueryWrapper<ApUser> apUserQueryWrapper = new LambdaQueryWrapper<>();
            apUserQueryWrapper.eq(ApUser::getPhone, loginDto.getPhone());
            //1.1查询手机号用户信息
            ApUser apUser = getOne(apUserQueryWrapper);
            if (apUser == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户信息不存在");
            }
            //1.2比对密码
            //获取前端传入的密码
            String password = loginDto.getPassword();
            //获取随机生成的加盐加密
            String salt = apUser.getSalt();
            //获取md5加密以后的密码(对前端传入的密码进行加盐加密操作)
            String digestAsHex = DigestUtils.md5DigestAsHex((password + salt).getBytes());
            if (!digestAsHex.equals(apUser.getPassword())) {
                //密码错误
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
            //1.3返回数据 生成jwt令牌
            //获取token
            String token = AppJwtUtil.getToken(Long.valueOf(apUser.getId()));
            Map<String, Object> map = new HashMap<>();
            map.put("token", token);
            //敏感数据置空
            apUser.setSalt("");
            apUser.setPassword("");
            map.put("user", apUser);
            return ResponseResult.okResult(map);
        } else {
            //2.没有账号密码,游客登录
            Map<String, Object> map = new HashMap<>();
            map.put("token", AppJwtUtil.getToken(0L));
            return ResponseResult.okResult(map);
        }
    }

    @Override
    public ResponseResult getAll(AuthDto authDto) {
        IPage page = new Page(authDto.getPage(), authDto.getSize());
        LambdaQueryWrapper<ApUser> queryWrapper = new LambdaQueryWrapper<>();
        if (authDto.getStatus() != null) {
            queryWrapper.eq(ApUser::getStatus, authDto.getStatus());
        }
        page = page(page, queryWrapper);
        ResponseResult responseResult = new PageResponseResult((int) page.getPages(), (int) page.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }
}
